
/*===============================================
	要完成四个功能：
	1. 已知坐标点，定位到图上		[ok]
	2. 已知坐标点，获取当前地理位置	[ok]
	3. 已知地理位置，获取实际坐标点	[ok]	
		<两种方式: 需要mark点,不需要mark点>
===============================================*/

var m_oListView = null;
var m_oAllMarks = null;	//当前覆盖物

function UpdateMark(oPoint, title, demo) 
{
	UpdateMarkPnt(oPoint, title, demo); 
	m_oMap.addEventListener(m_oMark, MConstants.DRAG_END, Event_DragEnd);
}

	//-拖放监听-
function Event_DragEnd(param) 
{
	m_oMark.lnglat = new MLngLat(param.eventX, param.eventY);
	$("#txtMapZB").val(param.eventX.substr(0, 10) + "," + param.eventY.substr(0, 9));
	

	SearchCoord([param.eventX, param.eventY], true, true);
}

function AddPoi(oPoint,id, bAddPoi)
{
   var markerOption = new MMarkerOptions(); 
		markerOption.imageUrl="http://code.mapabc.com/images/lan_1.png"; 
		markerOption.picAgent=false; 
		markerOption.imageAlign=MConstants.BOTTOM_CENTER; 
		markerOption.canShowTip= true; 
		markerOption.isEditable = false;//是否可以拖动
		markerOption.isDraggable = false;//是否可以拖动
		markerOption.rotation="60"; 
    var oMark = new MMarker(getLngLat(oPoint),markerOption); 
		oMark.id="marker"+id; 
	if(bAddPoi)
		m_oMap.addOverlay(oMark,false) ; 

	return oMark;
}

//-添加标记-
function addFandianMark(id, oPoint, title, demo)
{  
    var markerOption = new MMarkerOptions(); 
		//markerOption.imageUrl="http://code.mapabc.com/images/lan_1.png"; 
		markerOption.imageUrl="http://api.mapabc.com/flashmap/2.0/red.png"; 
		markerOption.picAgent=false; 
		markerOption.imageAlign=MConstants.BOTTOM_CENTER; 
		markerOption.canShowTip= true; 
		markerOption.isEditable = false;//是否可以拖动
		markerOption.isDraggable = false;//是否可以拖动
		markerOption.rotation="40"; 

	function addFlashTip()
	{   	
		var tipOption=new MTipOptions();   
		var fontstyle=new MFontStyle(); 
			fontstyle.size=14; 
			fontstyle.color=0xFF0066; 
			fontstyle.bold=false; 
		var fontstyle1=new MFontStyle(); 
			fontstyle1.size=20; 
			fontstyle1.color=0xFF0066;//内容字体颜色无效 
			fontstyle1.bold=true;   

		tipOption.title=title?title:"title"; 
		tipOption.titleFontStyle=fontstyle; 
		tipOption.content=demo?demo:"demo!"; 
		tipOption.contentFontStyle=fontstyle1; 
		tipOption.roundRectSize=20;//tip矩形圆边长度 
		tipOption.tipType=MConstants.FLASH_BUBBLE_TIP; 
		tipOption.hasShadow=true;   

		return tipOption;
	}
	var tipOption = addFlashTip(); 
    markerOption.tipOption = tipOption; 

    var oMark = new MMarker(getLngLat(oPoint), markerOption); 
		oMark.id=id; 		

	return oMark;
}

/*---------------------------------------------
				<<地址定位>>
1) 如果是完整的地址名，到了门牌号的，用新版
2) 如果是什么街，什么路的，用老版的可以定位
---------------------------------------------*/
	//-根据输入地址获取坐标-
function Cmd_Address() 
{
	var strAddress = $.trim($("#txtAddress").val());
	if (strAddress == "" || strAddress == "输入地址定位!") 
	{
		alert("请输入地址!");
		return ;
	}

	SearchAddress_All(cmd_AddressMatch_Callback_after, strAddress, getCurCityName());
}

function cmd_AddressMatch_Callback_after(x, y, grade, title)
{
	var oPoint = [getDecodeCoordinate(x), getDecodeCoordinate(y)];

	//更新mark
	UpdateMark(oPoint, title);

	//显示当前点
	$("#txtZB").val(oPoint[0] + "," + oPoint[1] + "," + grade);

	//-以当前点进行查询-
	SearchCoord(oPoint, true, true);
}

/*---------------------------------------------
				<<坐标查找>>
1) 坐标查找地标
2) 坐标查找类型数据
---------------------------------------------*/
//-只显示地址在文本框-
function Simple_Coord_Callback(data) 
{      
	//-将第一个显示在地图上-
	var sp = data.SpatialBean;
	var strAddress = sp.Province.name + sp.City.name + sp.District.name;
	if(sp.crossPoiList)
		strAddress += sp.crossPoiList[0].name;
	strAddress = strAddress + "("+sp.poiList["0"].address + sp.poiList["0"].name+")";
	$("#txtMapAddress").val(strAddress);
}

function JumpTo(nType, nID)
{
	var sID = "s"+nType;
		sID += nID;

	//-更新标签-
	var	obj = document.getElementById(sID);
	var oPoint = [$(obj).attr("cx"), $(obj).attr("cy")];
	UpdateMark(oPoint, $(obj).attr("name"), $(obj).attr("address"));

	//-显示当前的地图位置-
	SearchXzxqName(Simple_Coord_Callback, oPoint);
}

	//-根据输入坐标获取地址-
function Cmd_Coord() 
{
	var strCoord = $.trim($("#txtZB").val());
	if (strCoord == "") 
	{
		return;
	}
	var strSet = strCoord.split(/[,，|\s]/gi);
	if (strSet.length <= 1) 
	{
		return;
	}

	if (m_oMark) 
		UpdateMark(strSet);
	SearchCoord(strSet, true, true);
}

function SearchCoord(oPoint, bSearchName, bSearchPoi) 
{		
	var poixy = getLngLat(oPoint);//中心点坐标 

	if(bSearchName)
		SearchXzxqName(Coord_Callback, poixy);

	function Search2()
	{
		var poi_type = getCurDataType();
		if(poi_type=="不搜索")
			return;
	
		//chy-check 2013-08-23 只有搜索时才清空
		m_oListView.Clear();
		if(m_oAllMarks && m_oAllMarks.length>0)
		m_oMap.removeOverlays(m_oAllMarks);
	
		var strKey = $.trim($("#txtFandian").val());
		if(strKey=="请输入关键名进行搜索!")
			strKey="";
		PoiSearch(Coord_POI_Callback, getCurCityName(), strKey, poixy, poi_type);
	}
	
	
	if(bSearchPoi)
		Search2();
}

var	g_strTemplateItem_Coord = '<span id="s0{3}" class="s1" cx="{1}" cy="{2}" name="{4}" address="{5}"  filtertext="{4}|{5}"><b><a href="#" onclick="JumpTo(0, {3});">{0}</a></b> <br/>({1},{2})</span><hr/>';
function Coord_Callback(data) 
{      
	if (!data || data.message != "ok") 
	{
		alert("无此地理位置信息!");
		return;
	}

	//-将第一个显示在地图上-
	var sp = data.SpatialBean;
	var strAddress = sp.Province.name + sp.City.name + sp.District.name;
	if(sp.crossPoiList)
		strAddress += sp.crossPoiList[0].name;
	if(sp.poiList)
		strAddress = strAddress + "("+sp.poiList["0"].address + sp.poiList["0"].name+")";
	$("#txtMapAddress").val(strAddress);

	//-依次显示每个坐标-	
	var	strCur;
	var oItem = null;
	for (var i=0; i<sp.poiList.length; i++)
	{	
		var obj = sp.poiList[i];
		strCur = g_strTemplateItem_Coord.format(obj.address+obj.name, obj.x, obj.y, i+1, obj.name, obj.address);
		oItem = m_oListView.AddListItem();
		oItem.SetText(strCur);
	}
}

var	g_strTemplateItem_POI = '<span id="s1{6}" class="p1" title="类型:{4}" cx="{1}" cy="{2}" name="{0}" address="{7}"  filtertext="{0}|{7}">{6}. <b><a href="#" onclick="JumpTo(1, {6});">{0}</a></b>({1},{2}){5}{3}</span><hr/>';
function Coord_POI_Callback(data) 
{      
	 if(data.error_message != null)
	{
		alert("错误:" + data.error_message);
		return;
	}

	if(data.message!="ok")
	{
		alert("坐标查询失败，网络繁忙! data.message = " + data.message);
		return;
	}

	if(data.poilist.length==0)
	{
		alert("没有找到对应数据!");
		return;
	}

	//-依次显示每个坐标-
	var	strCur;
	var oItem = null;
	function getTel(obj)
	{
		var str="";

		if(obj.tel!="")
		{
			if(obj.address=="")
				str = "<br/>";	
			else
				str = ", ";
			
			str += "电话:"+obj.tel;			
		}
		
		return str;
	}

	var	oMarks = new Array();
	var oMark,obj;
	for (var i=0; i<data.poilist.length; i++)
	{	
		obj = data.poilist[i];
		strCur = g_strTemplateItem_POI.format
					(
						obj.name, 
						getDecodeCoordinate(obj.x), 
						getDecodeCoordinate(obj.y), 
						getTel(obj), 
						obj.type, 
						obj.address==""?"":"<br/>地址:"+obj.address,
						i+1,
						obj.address
					);
		oItem = m_oListView.AddListItem();
		oItem.SetText(strCur);
		oItem.m_oItemData = i+1;
		//oMark = AddPoi([obj.x, obj.y], i+1,false);
		oMark = addFandianMark("fandian_"+(i+1), [obj.x, obj.y], obj.name, obj.address);
		oMarks.push(oMark);
	}
	m_oMap.addOverlays(oMarks,false);
	m_oAllMarks = oMarks;
}


/*---------------------------------------------
				<<关键字查找>>
2) 目前指定餐饮服务
---------------------------------------------*/
function Cmd_Fandian() 
{
	var strKey = $.trim($("#txtFandian").val());
	if (strKey == "") 
		return;
	
	var type = getCurDataType();
	if(type=="不搜索")
		return;
	
	m_oListView.Clear();
	KeySearch(Coord_POI_Callback, getCurCityName(), strKey, type);
}

function getCurCityName()
{
	var strCityName = document.getElementById("citySelect").value;

	return strCityName;
}

function getCurDataType()
{
	var strType;	
	var oSelect = document.getElementById("cboType");	
	if(oSelect.selectedIndex>0)	
	{
		strType = oSelect.options[oSelect.selectedIndex].text;
	}

	return strType;
}

//-城市切换-
function tocity_onchange(strCurCity)
{
	//-跳转到该城市中心点-
	var strJumpCity = strCurCity;
	if(strCurCity.charAt(strCurCity.length-1)=="市")
		strJumpCity = strCurCity.substr(0,strCurCity.length-1); 
	var maptools=new MMapTools(m_oMap); 
    maptools.setCenterByCity(strJumpCity); 

	//-显示隐藏菜单状态-
	$("li[id^='menu']").css("display","none");
	var strCur = "li[id^='menu"+strJumpCity+"']";
	$(strCur).css("display","block");
}

$(document).ready(function () 
{
	//-input address prompt-
	txtTipInput("txtAddress", "输入地址定位!");
	txtTipInput("txtZB", "输入坐标定位,格式:116.384,39.234!");
	txtTipInput("txtFandian", "请输入关键名进行搜索!");

	$("#spanAddress").bind("click", Cmd_Address);
	$("#spanCoord").bind("click", Cmd_Coord);
	$("#spanFandian").bind("click", Cmd_Fandian);

	//将地图中心点移到标签处
	$("#spanMark").bind("click", function(e)
	{
		m_oMap.setCenter(m_oMark.lnglat);
	});

	//标签移到中心点
	$("#spanCenter").bind("click", function(e)
	{
		var oCenterPt = m_oMap.getCenter();
		$("#txtMapZB").val(oCenterPt.lngX + "," + oCenterPt.latY);
		UpdateMark(oCenterPt);
		SearchXzxqName(Simple_Coord_Callback, oPoint);
	});

	$("#txtAddress").keydown(function (e) 
	{
		if (e.keyCode == 13) 
		{
			Cmd_Address();
		}
	});
	$("#txtZB").keydown(function (e) 
	{
		if (e.keyCode == 13) 
		{
			Cmd_Coord();
		}
	});
	$("#txtFandian").keydown(function (e) 
	{
		if (e.keyCode == 13) 
		{
			Cmd_Fandian();
		}
	});
	
	//-初始化地图-
	var oPoint = [116.3974,39.910263];
	m_oMap = CreateMap("divMap", oPoint);
	m_oMark = CreateMark(m_oMap, oPoint);
	m_oMap.addEventListener(m_oMark, MConstants.DRAG_END, Event_DragEnd);

	//-鼠标单击-
	function MclickMouse(param)
	{   
		var xy = "jw=("+param.eventX+","+param.eventY+")";
		$("#txtMapAddress").val(xy);	
	}
	m_oMap.addEventListener(m_oMap, MConstants.MOUSE_CLICK, MclickMouse);//鼠标点击事件 

	//-初始化结果列表栏-
	m_oListView = new CListView("divResult",false);	
});


//-范围获取-
$(document).ready(function () 
{
	$("ul[id='navigation']>li").bind("mouseover", function()
	{
		displaySubMenu(this);
	}); 

	$("ul[id='navigation']>li").bind("mouseout", function()
	{
		hideSubMenu(this);
	}); 
});